//班组名字
var teamName = "所有班组";
var teamNameArray = [];
$.get('/system/order/getWorkGroupNames',function(res){
    teamNameArray = res.data;
    $('#ex1sub').append('<button onclick="switchTeam(\'所有班组\')">所有班组</button>');
    $.each(teamNameArray,function(i){
        $('#ex1sub').append('<button onclick="switchTeam(\''+teamNameArray[i].trim()+'\')">'+teamNameArray[i]+'</button>');
    })
});
function switchTeam(team){
    teamName = team;
    $('#switchTeam').text(team);
    $('#ex1 a').trigger('click');
    $("table").find('tr').slice(1).remove();
    Cookies.set('teamname',team);
}
$(document).ready(function (){
    var _tn = Cookies.get('teamname');
    if(_tn != undefined && _tn != ""){
        switchTeam(_tn);
    }else {
        switchTeam('所有班组')
    }
});
//加载时间
function loadtm() {
    var days = new Array("日", "一", "二", "三", "四", "五", "六");
    var currentDT = new Date();
    var y, m, date, day, hs, ms, ss, theDateStr;
    y = currentDT.getFullYear(); //四位整数表示的年份
    m = currentDT.getMonth(); //月
    date = currentDT.getDate(); //日
    day = currentDT.getDay(); //星期
    hs = currentDT.getHours(); //时
    ms = currentDT.getMinutes(); //分
    ss = currentDT.getSeconds(); //秒
    theDateStr = y + "年" + m + "月" + date + "日 星期" + days[day] + " " + hs + ":" + ms + ":" + ss;
    //theDateStr = " "+p(hs)+":"+p(ms)+":"+p(ss);
    //console.log(theDateStr)
    $(".tmtext span").html(theDateStr);
    // setTimeout 在执行时,是在载入后延迟指定时间后,去执行一次表达式,仅执行一次

}

function p(s) {
    return s < 10 ? '0' + s : s;
}
window.setInterval(loadtm, 1000);
var messageArry = new Array();

//对任务进行处理

function handelMessage() {

    if (messageArry.length == 0) {
        return 1;
    }
    var messageArry_temp = messageArry;
    for (i = 0; i < messageArry_temp.length; i++) {

        var workStatusName = "未知";
         //0.未锁定1.已排程2.已锁定3.已派工4.已下达5.开工6.报工7.完工8.停工9.已暂停
         switch (messageArry_temp[i].workStatus){
                case "0":
                    workStatusName="未锁定";
                case "1":
                    workStatusName = "已排程";
                    break;
                case "2":
                    workStatusName = "已锁定";
                    break;
                case "3":
                    workStatusName = "已派工";
                    break;
                case "4":
                    workStatusName = "已下达";
                    break;
                case "5":
                    workStatusName = "开工";
                    break;
                case "6":
                    workStatusName = "报工";
                    break;
                case "7":
                    workStatusName = "完工";
                    break;
                case "8":
                    workStatusName = "停工";
                    break;
                case "9":
                    workStatusName = "已暂停";
                    break;
                default:
                    workStatusName="未知";
                    break;
            }
        var tableContent =  '<tr class="orderContent">' +
                            '<td class="teamName">'+messageArry_temp[i].teamName+'</th>'+
                            '<td class="OrderNo">'+messageArry_temp[i].orderNo+'</th>'+
                            '<td class="OrderLine">'+messageArry_temp[i].orderLine+'</th>'+
                            '<td class="CustomerProducts">'+messageArry_temp[i].customerProducts+'</th>'+
                            '<td class="Category">'+messageArry_temp[i].category+'</th>'+
                            '<td class="GoodName">'+messageArry_temp[i].goodName+'</th>'+
                            '<td class="CustomerDate">'+messageArry_temp[i].customerDate.slice(0,10)+'</th>'+
                            '<td class="WorkLine">'+messageArry_temp[i].workLine+'</th>'+
                            '<td class="WorkName">'+messageArry_temp[i].workName+'</th>'+
                            '<td class="workStatusName">'+workStatusName+'</th></tr>';
        $('table>tbody tr:first').after(tableContent);
       /* var msg = new SpeechSynthesisUtterance("新通知，客户产品"+messageArry_temp[i].customerProducts+"工序为"+workStatusName);
        msg.lang = 'zh-CN'
        msg.voice = speechSynthesis.getVoices().filter(function (voice) {
            return voice.name == 'Whisper';
        })[0];
        speechSynthesis.speak(msg);*/
        //只保留表格中的前15条
        $("table").find('tr').slice(15,-1).remove()
        //将消息队列中的第一个给删除了
        messageArry.shift();
    }
}

//有信息的消息过来时进行播报
function msgArrival(message) {
	console.log(message);
	if(message.teamName == teamName || teamName == "所有班组"){
        //将最新的消息推送到队列尾部
        messageArry.push(message);
        handelMessage();
    }

}
var initWebSocket = function(){
    var resVal = null;
    var stompClient = null;
    var socket = new SockJS('/websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({},
    function (frame) {
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/publishNewOrder_WebSocket', function (data) {
            if(data.body == "Refresh"){
                window.location.reload(true);
            }
        	msgArrival(JSON.parse(data.body));
        });
    }
    );
    return stompClient;
}

var websocketClient = initWebSocket();

var timer = setInterval(function(){
    if(!websocketClient.connected){
        websocketClient = initWebSocket();
    }
},3000);